package com.sikaryofficial.workbench.utils;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.sikaryofficial.workbench.domain.entity.PriceTier;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author : wuweihong
 * @desc : 定义计算的表达式
 *  todo 价格的加减通过decimal计算，保留的小数位与产品确认
 * @date : 2024-09-27
 */



public class QLExpressUtils {

    /**
     * 获取阶梯价
     */
    public static BigDecimal calculatePriceByPriceTier(Long goodsId, Map<Long, List<PriceTier>> priceRanges, int quantity, String expression) {
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<>();
        context.put("quantity", quantity);
        context.put("priceRanges", priceRanges.get(goodsId));
        try {
            Object result = runner.execute(expression, context, null, true, false);
            return new BigDecimal(String.valueOf(result));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 计算子订单总价
     * @param unitPrice
     * @param quantity
     * @return
     */
    public static BigDecimal calculateTotalPrice(BigDecimal unitPrice, long quantity) {
        String expression = "quantity * unitPrice";
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("quantity", quantity);
        context.put("unitPrice", unitPrice);
        ExpressRunner runner = new ExpressRunner();
        try{
            Object result = runner.execute(expression, context, null, true, false);
            return new BigDecimal(String.valueOf(result));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static BigDecimal calculateUpdatePrice(BigDecimal oldAmount, BigDecimal oldItemTotalPrice, BigDecimal newItemTotalPrice) {
        String expression = "oldAmount + (newItemTotalPrice - oldItemTotalPrice)";
        DefaultContext<String, Object> context = new DefaultContext<>();
        context.put("oldAmount", oldAmount);
        context.put("oldItemTotalPrice", oldItemTotalPrice);
        context.put("newItemTotalPrice", newItemTotalPrice);
        ExpressRunner runner = new ExpressRunner();
        try {
            Object result = runner.execute(expression, context, null, true, false);
            return new BigDecimal(String.valueOf(result));
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static BigDecimal calculateGoodsPrice(List<BigDecimal> itemTotalList) {
        String expression = StringUtils.join(itemTotalList, " + ");
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        ExpressRunner runner = new ExpressRunner();
        try{
            Object result = runner.execute(expression, context, null, true, false);
            return new BigDecimal(String.valueOf(result));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static BigDecimal calculateDiscount(BigDecimal amountNoDiscount, BigDecimal amountDiscount) {
        String expression = "amountNoDiscount - amountDiscount";
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        context.put("amountNoDiscount", amountNoDiscount);
        context.put("amountDiscount", amountDiscount);
        ExpressRunner runner = new ExpressRunner();
        try{
            Object result = runner.execute(expression, context, null, true, false);
            return new BigDecimal(String.valueOf(result));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
